package ecitys.commons
import grails.converters.*
class UfController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [ufInstanceList: Uf.list(params), ufInstanceTotal: Uf.count()]
    }
	
	def dataDrillDown = {
        render(template: 'drillDown', var: 'demo', bean: Uf.get(params.id))
    }
	
	def listAsJSON = {
	    
        params.max = Math.min(params.max ? Integer.parseInt(params.max) : 10, 100)
		def queryResult = Uf.list(params)
		response.setHeader("Cache-Control", "no-store")
		def list = []
		queryResult.each{
		    list << [
			         id:it.id,
					 codigoUF:it.codigoUF,
					 nomeUF:it.nomeUF,
					 dataUrl: g.createLink(action: 'show') + "/$it.id"
			         ]
		}
		def data = [
                totalRecords: Uf.count(),
                results: list
        ]
        render data as JSON
		
	}

    def create = {
        def ufInstance = new Uf()
        ufInstance.properties = params
        return [ufInstance: ufInstance]
    }

    def save = {
        def ufInstance = new Uf(params)
        if (ufInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'uf.label', default: 'Uf'), ufInstance.id])}"
            redirect(action: "show", id: ufInstance.id)
        }
        else {
            render(view: "create", model: [ufInstance: ufInstance])
        }
    }

    def show = {
        def ufInstance = Uf.get(params.id)
        if (!ufInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'uf.label', default: 'Uf'), params.id])}"
            redirect(action: "list")
        }
        else {
            [ufInstance: ufInstance]
        }
    }

    def edit = {
        def ufInstance = Uf.get(params.id)
        if (!ufInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'uf.label', default: 'Uf'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [ufInstance: ufInstance]
        }
    }

    def update = {
        def ufInstance = Uf.get(params.id)
        if (ufInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (ufInstance.version > version) {
                    
                    ufInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'uf.label', default: 'Uf')] as Object[], "Another user has updated this Uf while you were editing")
                    render(view: "edit", model: [ufInstance: ufInstance])
                    return
                }
            }
            ufInstance.properties = params
            if (!ufInstance.hasErrors() && ufInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'uf.label', default: 'Uf'), ufInstance.id])}"
                redirect(action: "show", id: ufInstance.id)
            }
            else {
                render(view: "edit", model: [ufInstance: ufInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'uf.label', default: 'Uf'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def ufInstance = Uf.get(params.id)
        if (ufInstance) {
            try {
                ufInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'uf.label', default: 'Uf'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'uf.label', default: 'Uf'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'uf.label', default: 'Uf'), params.id])}"
            redirect(action: "list")
        }
    }
}
